﻿# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.itemDao import ItemDao

from . import app

CORS(app, supports_credentials=True)

__author__ = "Prince_CHEN"


@app.route('/insertItem', methods=['POST'])
def insert_itemNoToken():
    """
    添加信息
    :return:
    """
    if not request.json:
        abort(400)
    info = {
        'deptId': request.json.get('deptId', 0),
        'itemHandlerName': request.json.get('itemHandlerName', ''),
        'itemHandlerPhone': request.json.get('itemHandlerPhone', ''),
        'itemTitle': request.json.get('itemTitle', ''),
        'itemPriceRange': request.json.get('itemPriceRange', ''),
        'itemContent': request.json.get('itemContent', ''),
        'itemUrl': request.json.get('itemUrl', ''),
        'itemDeadline': request.json.get('itemDeadline', None),
        'itemPublishdate': request.json.get('itemPublishdate', None),
        'itemAddress': request.json.get('itemAddress', ''),
        'itemStatus': request.json.get('itemStatus', 0),
        'serviceId': request.json.get('serviceId', 0),
        'itemEnterPerson': request.json.get('itemEnterPerson', ''),
        'itemEnterTime': request.json.get('itemEnterTime', None),
        'checkStatusOne': request.json.get('checkStatusOne', 0),
        'checkRemarkOne': request.json.get('checkRemarkOne', ''),
        'checkPersonOne': request.json.get('checkPersonOne', ''),
        'checkTimeOne': request.json.get('checkTimeOne',None),
        'checkStatusTwo': request.json.get('checkStatusTwo', 0),
        'checkRemarkTwo': request.json.get('checkRemarkTwo', ''),
        'checkTimeTwo': request.json.get('checkTimeTwo', None),
        'checkPersonTwo': request.json.get('checkPersonTwo', '')
    }

    rs, info = ItemDao().insertItem(info)
    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findItemById', methods=['POST'])
def find_item_by_idNoToken():
    """
    根据ID查询
    :return:
    """
    if not request.json:
        abort(400)
    itemId = request.json.get('itemId', '')
    info = ItemDao().findItemById(itemId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/queryItemByCondition', methods=['POST'])
def find_item_by_conditionNoToken():
    """
    条件查询
    :return:
    """
    if not request.json:
        abort(400)
    try:
        page = request.json['page']
        pageIndex = page["pageIndex"]
        pageSize = page["pageSize"]
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 pageIndex和pageSize"}})
    try:
        conditions = request.json['condition']
    except:
        return jsonify({"message": {"code": 0, "status": "未传入指定condition"}})
    try:
        multiSort = request.json['multiSort']
    except:
        return jsonify({"message": "未传入排序规则multiSort"})

    info, total = ItemDao().findItemByCondition(pageIndex=pageIndex, pageSize=pageSize, conditions=conditions,
                                                multiSort=multiSort)

    if info:
        return jsonify({"total": total, "info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"info":[], "message": {"code": 0, "status": "error"}})


@app.route('/deleteItemByIds', methods=['POST'])
def delete_item_by_idsNoToken():
    """
    根据ID删除，ID以数组形式传递
    :return:
    """
    if not request.json:
        abort(400)
    idArray = request.json.get('idArray', [0])

    info = ItemDao().deleteItem(idArray)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/updateItemById', methods=['POST'])
def update_item_by_idNoToken():
    """
    更新
    :return:
    """
    if not request.json:
        abort(400)
    try:
        itemId = request.json['itemId']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入要修改信息的ID"}})
    oldInfo = ItemDao().selectItem(itemId)
    info = {
        'itemId': request.json['itemId'],
        'deptId': request.json.get('deptId', oldInfo[1]),
        'itemHandlerName': request.json.get('itemHandlerName', oldInfo[2]),
        'itemHandlerPhone': request.json.get('itemHandlerPhone', oldInfo[3]),
        'itemTitle': request.json.get('itemTitle', oldInfo[4]),
        'itemPriceRange': request.json.get('itemPriceRange', oldInfo[5]),
        'itemContent': request.json.get('itemContent', oldInfo[6]),
        'itemUrl': request.json.get('itemUrl', oldInfo[7]),
        'itemDeadline': request.json.get('itemDeadline', oldInfo[8]),
        'itemPublishdate': request.json.get('itemPublishdate', oldInfo[9]),
        'itemAddress': request.json.get('itemAddress', oldInfo[10]),
        'itemStatus': request.json.get('itemStatus', oldInfo[11]),
        'serviceId': request.json.get('serviceId', oldInfo[12]),
        'itemEnterPerson': request.json.get('itemEnterPerson', oldInfo[13]),
        'itemEnterTime': request.json.get('itemEnterTime', oldInfo[14]),
        'checkStatusOne': request.json.get('checkStatusOne', oldInfo[15]),
        'checkRemarkOne': request.json.get('checkRemarkOne', oldInfo[16]),
        'checkPersonOne': request.json.get('checkPersonOne', oldInfo[17]),
        'checkTimeOne': request.json.get('checkTimeOne', oldInfo[18]),
        'checkStatusTwo': request.json.get('checkStatusTwo', oldInfo[19]),
        'checkRemarkTwo': request.json.get('checkRemarkTwo', oldInfo[20]),
        'checkPersonTwo': request.json.get('checkPersonTwo', oldInfo[21]),
        'checkTimeTwo': request.json.get('checkTimeTwo', oldInfo[22])
    }

    info = ItemDao().updateItem(info)
    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "No changes"}})
